package studio.raptor.ddal.core.log.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.core.log.bo.ConnPoolLogData;
import studio.raptor.ddal.core.log.bo.SqlProcessLogData;
import studio.raptor.ddal.core.log.common.LogClientConstant;
import studio.raptor.ddal.core.log.service.interfaces.ILogDealServ;
import studio.raptor.ddal.core.log.service.interfaces.ILogProcess;
import studio.raptor.ddal.core.log.service.interfaces.IThreadManagerService;

/**
 * Created by liujy on 2018/1/15.
 */
public class LogProcessImpl implements ILogProcess {

    private static Logger logger = LoggerFactory.getLogger(LogProcessImpl.class);
    private ILogDealServ logDealServ;
    private IThreadManagerService threadService;
    private boolean isInit=false;

    public LogProcessImpl(){
        init();
    }

    public void init(){
        if("y".equalsIgnoreCase( LogClientConstant.logSwitch )){
            if(logDealServ==null) logDealServ=new LogDealServImpl();
            if(threadService==null){
                threadService=new LogThreadManagerServiceImpl();
                threadService.start();
            }
            isInit=true;
        }
    }

    @Override
    public void process(Object logObj) {
        if(!isInit) init();
        if(isInit){
            if(logObj instanceof SqlProcessLogData){
                logDealServ.logDeal( logObj, LogClientConstant.LOG_TYPE_SQL_PROCESS );
            }
            else if(logObj instanceof ConnPoolLogData){
                logDealServ.logDeal( logObj, LogClientConstant.LOG_TYPE_CONN_POOL );
            }
        }
    }


}
